#include <cstdlib>
#include <SFML/Graphics.hpp>
#include "SFML_mod.hh"
#include <sstream>

using namespace sf;

SFML_mod::SFML_mod(RenderWindow *app)
{
  this->app = app;
}

SFML_mod::~SFML_mod()
{
  delete this->app;
}
  
void SFML_mod::print_map(int x, int y)
{    
    int sizeX, sizeY;
    sizeX = 5 + (x * 10);
    sizeY = 5 + (y * 10);
    
    Shape map;
    map.AddPoint(5, 5, Color(255, 255, 0), Color(255,255,255));
    map.AddPoint(sizeY, 5, Color(255, 255, 0), Color(255,0,0));
    map.AddPoint(sizeY, sizeX, Color(0, 255, 255), Color(0,255,0));
    map.AddPoint(5, sizeX, Color(0, 255, 255), Color(0,0,255));
    map.EnableFill(false);   // Remplissage activé
    map.EnableOutline(true); // Bordures activées
    map.SetOutlineWidth(5);  // Bordures de taille 5 pixels

    this->app->Clear();
    this->app->Draw(map);
}

void SFML_mod::print_drone(int x, int y)
{
    int sizeX, sizeY;
    sizeX = 5 + (x * 10);
    sizeY = 5 + (y * 10);
    
    Shape drone;

    drone.AddPoint(sizeY + 2, sizeX + 2, Color(0, 255, 0), Color(0, 255, 0));
    drone.AddPoint(sizeY + 2, sizeX + 8, Color(0, 255, 0), Color(0, 255, 0));
    drone.AddPoint(sizeY + 8, sizeX + 8, Color(0, 255, 0), Color(0, 255, 0));
    drone.AddPoint(sizeY + 8, sizeX + 2, Color(0, 255, 0), Color(0, 255, 0));
    drone.EnableFill(false);
    drone.EnableOutline(true); // Bordures activées
    drone.SetOutlineWidth(2);  // Bordures de taille 2 pixels
  
    this->app->Draw(drone);
}

void SFML_mod::print_stone(int x, int y, int stones)
{
    int sizeX, sizeY;
    int R, G, B;
    sizeX = 5 + (x * 10);
    sizeY = 5 + (y * 10);
  
    if (stones == 0) //nemalite
    {
        R = 205;
        G = 186;
        B = 150;
    }
    else if (stones == 1) // emeraude
    {   
        R = 78;
        G = 238;
        B = 148;
    }
    else if (stones == 2) //rubis
    {
        R = 255;
        G = 0;
        B = 0;
    }
    else if (stones == 3) // topaz
    {
        R = 238;
        G = 154;
        B = 73;
    }
    else if (stones == 4) //saphir
    {
        R = 28;
        G = 134;
        B = 238;
    }
    else //amethyste
    {
        R = 122;
        G = 103;
        B = 238;
    }

    Shape stone;
    stone.AddPoint(sizeY, sizeX, Color(R, G, B), Color(R, G, B));
    stone.AddPoint(sizeY, sizeX + 10, Color(R, G, B), Color(R, G, B));
    stone.AddPoint(sizeY + 10, sizeX + 10, Color(R, G, B), Color(R, G, B));
    stone.AddPoint(sizeY + 10, sizeX, Color(R, G, B), Color(R, G, B));
    stone.EnableFill(true);
  
    this->app->Draw(stone);
}

#include <cstdio>

int SFML_mod::get_key()
{
  Event	event;

  while (this->app->GetEvent(event))
    {
      if (event.Type == Event::KeyPressed)
	{
	  if (event.Key.Code == 256)
           return 27;
        }
  return -1;
}


void	SFML_mod::init(int x, int y)
{
int sizeX, sizeY;
  sizeX = x * 10;
  sizeY = y * 10;

  this->app = new RenderWindow(VideoMode(sizeY + 170, sizeX + 10, 32), "Visualisator zappy");
}

void	SFML_mod::close()
{
  this->app->Close();
}
  
extern "C" SFML_mod	*Display_builder()
{
  return new SFML_mod(new RenderWindow);
}
